<?php
include "forum/smilies.php";
include "forum/connect.php";
include "forum/admin/var.php";

if(isset($_SESSION['user']))
{
  $user=$_SESSION['user'];
  $getuser="SELECT * from b_users a, b_templates b where b.templateid=a.templateclass and a.username='$user'";
  $getuser2=mysql_query($getuser) or die("Could not get user info");
  $getuser3=mysql_fetch_array($getuser2);
  $thedate=date("U");
  $checktime=$thedate-200;
  $uprecords="Update b_users set lasttime='$thedate' where userID='$getuser3[userID]'";
  mysql_query($uprecords) or die("Could not update records");
  if($getuser3[tsgone]<$checktime)
  {
    $updatetime="Update b_users set tsgone='$thedate', oldtime='$getuser3[tsgone]' where userID='$getuser3[userID]'";
    mysql_query($updatetime) or die("Could not update time");
  }
  $templateclass=$getuser3['templatepath'];
}
//some settings above arryes
print "<link rel='stylesheet' href='news/style.css' type='text/css'>"; //chooses which template to display
if(strlen($getuser3[username])<1)
{
  $getuser3[status]=-1;
}
 
//you looking at comments
if(isset($_GET['news']) && isset($_GET['page'])) 
 {
   $page=$_GET['page'];
   $news=$_GET['news'];
   $post=$_GET['post'];
   $user=$_SESSION['user'];
   $selection="SELECT * from b_news where id=$news";
   $selection2=mysql_query($selection);
  
   print "<table class='maintable' cellspacing='0'>";
   print "<tr><td class='headline' colspan='6'><tr><td valign='top' class='forumrow'><div class='links2'>"; ?>
   <a href="javascript:get('<?php print "$sitepath"; ?>news.php', 'news');" >
   <?php print "&nbsp;&nbsp;&nbsp;<img src='news/back-icon.gif' /> Go Back</a>";
         print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='news/addcomment.php?news=$news'><img src='news/index-icon.gif' /> Add Comment</a></div>";

   $getforuminfo="SELECT * from b_news where id='$news'";
   $getforuminfo2=mysql_query($getforuminfo) or die("Could not get forum info");
   $getforuminfo3=mysql_fetch_array($getforuminfo2);
   
 
   print "</td></tr><tr>";
   while($selection3=mysql_fetch_array($selection2))
      {
         print "<td valign='top' class='forumrow'>"; $getforuminfo3[content]=BBCode($getforuminfo3[content]);
		 print "<b>$getforuminfo3[title]</b><br><div style='font-size:11px'>$getforuminfo3[content]</div><div class='comm'>Posted by $getforuminfo3[author] on $getforuminfo3[datepost]&nbsp;</div>";
	     $getcomm="SELECT * from b_comments where newsid='$news' order by timepost $commorder limit $page,$limit";
         $getcomm2=mysql_query($getcomm) or die("Could not get forum info");
         while($getcomm3=mysql_fetch_array($getcomm2))
		  {
		   
		   print "<div style=' font-size: 11px; color:#ffffff'><em><strong>$getcomm3[poster]</strong> @ $getcomm3[datepost] says:</em>";

		   print "</div>";
		   print "<div style='font-size: 9px; margin-left: 8px; color:#cccccc'>$getcomm3[content]</div>"; 
		 }

         
      }
	
include "news/paginate.php"; 
 print "</td></tr></table>";

}
else //looking at main index
{     if($getuser3[status]>=3)
      {
        print "<div class='button'><center><A href='forum/admin/addnews.php'>Add News</a></center></span></div>";
      }
      $forumselect1="SELECT * from b_news order by timepost DESC limit $newslimit";
      $forumselect2=mysql_query($forumselect1);
      print "<table class='maintable' cellspacing='0'>";
        while($forumselect3=mysql_fetch_array($forumselect2))
        {
		    $ico=$forumselect3[iconid];
            print "<tr><td valign='center' class='forumrow' width='1%'><img src='news/id$ico.gif' /></td><td valign='center' class='forumrow'width='99%'>"; ?>
			<a href="javascript:get('<?php print "$sitepath"; ?>news.php?news=<?php print "$forumselect3[id]" ?>&page=0', 'news');">
			<?php print "<b>$forumselect3[title]</b></a></td></tr><tr><td valign='top' class='forumrow' colspan='2'>"; 
			      $forumselect3[content]=BBCode($forumselect3[content]);
			      print "<div style='font-size:11px'>$forumselect3[content]</div>";
				  $ccount1 = "SELECT COUNT(*) FROM b_comments where newsid='$forumselect3[id]'";
				  $ccount2=mysql_query($ccount1) or die(mysql_error());
				  $ccount= mysql_result($ccount2, 0); 
				  print "<div class='comm2'>"; ?>
				  <a href="javascript:get('<?php print "$sitepath"; ?>news.php?news=<?php print "$forumselect3[id]" ?>&page=0', 'news');">
		    <?php print "&nbsp;$ccount Comments</a></div>";
				  print "<div class='comm'>Posted by $forumselect3[author] on $forumselect3[datepost]&nbsp;</div>";
			
            print "</td></tr>";
          
        }
        if(mysql_num_rows($forumselect2) > 0)
        {
          mysql_data_seek($forumselect2,0);
        }}
print "</table>";

?>

<?php
//function for getting member status
function getstatus($statnum)
{
  if ($statnum==0)
  {
     return "members";
  }
  else if($statnum==1)
  {
     return "moderators";
  }
  else if($statnum==2)
  {
    return "supermoderators";
  }
  else if($statnum==3)
  {
    return "administrators";
  }
  else if($statnum==4)
  {
    return "Head Administrator";
  }
}
?> 
 
<?php
//function for getting ranks
   function getrank($numposts, $thequery)
   {
      while($therank=mysql_fetch_array($thequery))
      {
        if($numposts>=$therank[postsneeded])
        { 
           $rank=$therank[rankname];
        }
      }
      return $rank;
   }
?>

<? //BBCODE function
	//Local copy

	function BBCode($Text)
	    {
        	// Replace any html brackets with HTML Entities to prevent executing HTML or script
            // Don't use strip_tags here because it breaks [url] search by replacing & with amp
     


            // Convert new line chars to html <br /> tags
            $Text = nl2br($Text);

            // Set up the parameters for a URL search string
            $URLSearchString = " a-zA-Z0-9\:\&\/\-\?\.\=\_\~\#\'";
            // Set up the parameters for a MAIL search string
            $MAILSearchString = $URLSearchString . " a-zA-Z0-9\.@";

            // Perform URL Search
            $Text = preg_replace("(\[url\]([$URLSearchString]*)\[/url\])", '<a href="$1">$1</a>', $Text);
            $Text = preg_replace("(\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[/url\])", '<a href="$1" target="_blank">$2</a>', $Text);
            $Text = preg_replace("(\[URL\=([$URLSearchString]*)\]([$URLSearchString]*)\[/URL\])", '<a href="$1" target="_blank">$2</a>', $Text);
            // Perform MAIL Search
            $Text = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])", '<a href="mailto:$1">$1</a>', $Text);
            $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.+?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);

            // Check for bold text
            $Text = preg_replace("(\[b\](.+?)\[\/b])is",'<b>$1</b>',$Text);

            // Check for Italics text
            $Text = preg_replace("(\[i\](.+?)\[\/i\])is",'<I>$1</I>',$Text);

            // Check for Underline text
            $Text = preg_replace("(\[u\](.+?)\[\/u\])is",'<u>$1</u>',$Text);

            // Check for strike-through text
            $Text = preg_replace("(\[s\](.+?)\[\/s\])is",'<span class="strikethrough">$1</span>',$Text);

            // Check for over-line text
            $Text = preg_replace("(\[o\](.+?)\[\/o\])is",'<span class="overline">$1</span>',$Text);

            // Check for colored text
            $Text = preg_replace("(\[color=(.+?)\](.+?)\[\/color\])is","<span style=\"color: $1\">$2</span>",$Text);

            // Check for sized text
            $Text = preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is","<span style=\"font-size: $1px\">$2</span>",$Text);

            // Check for list text
            $Text = preg_replace("/\[list\](.+?)\[\/list\]/is", '<ul class="listbullet">$1</ul>' ,$Text);
            $Text = preg_replace("/\[list=1\](.+?)\[\/list\]/is", '<ul class="listdecimal">$1</ul>' ,$Text);
            $Text = preg_replace("/\[list=i\](.+?)\[\/list\]/s", '<ul class="listlowerroman">$1</ul>' ,$Text);
            $Text = preg_replace("/\[list=I\](.+?)\[\/list\]/s", '<ul class="listupperroman">$1</ul>' ,$Text);
            $Text = preg_replace("/\[list=a\](.+?)\[\/list\]/s", '<ul class="listloweralpha">$1</ul>' ,$Text);
            $Text = preg_replace("/\[list=A\](.+?)\[\/list\]/s", '<ul class="listupperalpha">$1</ul>' ,$Text);
            $Text = str_replace("[*]", "<li>", $Text);
             $Text = preg_replace("(\[quote\](.+?)\[\/quote])is",'<center><table class="quotecode"><tr row="forumrow"><td>Q u o t e:<br>$1</td></tr></table></center>',$Text);
            $Text = preg_replace("(\[code\](.+?)\[\/code])is",'<center><table class="quotecode"><tr row="forumrow"><td>C o d e:<br>$1</td></tr></table></center>',$Text);

            // Check for font change text
            $Text = preg_replace("(\[font=(.+?)\](.+?)\[\/font\])","<span style=\"font-family: $1;\">$2</span>",$Text);

            //check for items ids
            $Text = preg_replace("(\[item=(.+?),(.+?)\](.+?)\[\/item\])","<a href=\"#\" type=\"http://www.wowhead.com/?item=$1;\" style=\"color: $2;\" onclick=\"return false;\">$3</a>",$Text);

            // Images
            // [img]pathtoimage[/img]
            $Text = preg_replace("/\[IMG\](.+?)\[\/IMG\]/", '<img src="$1">', $Text);
            $Text = preg_replace("/\[img\](.+?)\[\/img\]/", '<img src="$1">', $Text);
            // [img=widthxheight]image source[/img]
            $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.+?)\[\/img\]/", '<img src="$3" height="$2" width="$1">', $Text);

	        return $Text;
		}
?>
